package com.zlq.controller;

import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import sun.applet.Main;


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;

import org.apache.commons.io.FileUtils;


@Controller
public class FileDownload {


//    @GetMapping("download/{filename}")
//    public ResponseEntity<byte[]> download(@PathVariable String filename) throws IOException {
//        //下载文件的路径(这里绝对路径)
//        String filepath= "F:\\download\\"+filename;
//        File file =new File(filepath);
//        //创建字节输入流，这里不实用Buffer类
//        InputStream in = new FileInputStream(file);
//        //available:获取输入流所读取的文件的最大字节数
//        byte[] body = new byte[in.available()];
//        //把字节读取到数组中
//        in.read(body);
//        //设置请求头
//        MultiValueMap<String, String> headers = new HttpHeaders();
//        headers.add("Content-Disposition", "attchement;filename=" + file.getName());
////        headers.add("Content-Disposition", "attchement;filename=" + URLEncoder.encode(file.getName(), "UTF-8"));
//        //设置响应状态
//        HttpStatus statusCode = HttpStatus.OK;
//        in.close();
//        ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(body, headers, statusCode);
//        return entity;//返回
//    }
    @GetMapping("download/{filename}")
    public ResponseEntity<byte[]> download(@PathVariable String filename) throws IOException {


        System.out.println(filename);
        File file =new File("F:/download/"+filename+".png");

        System.out.println(file.getName());
            byte[] bytes = FileUtils.readFileToByteArray(new File("F:/download/"+file.getName()));
            HttpHeaders headers=new HttpHeaders();
//            headers.set("Content-Disposition","attachment;filename="+file.getName());
        headers.add("Content-Disposition", "attchement;filename=" + URLEncoder.encode(file.getName(), "UTF-8"));
            ResponseEntity<byte[]> entity=new ResponseEntity<>(bytes,headers,HttpStatus.OK);
            return entity;



        //下载文件的路径(这里绝对路径)
//        String filepath= "F:/download/"+filename;
//        File file =new File(filepath);
//        //创建字节输入流，这里不实用Buffer类
//        InputStream in = new FileInputStream(file);
//        //available:获取输入流所读取的文件的最大字节数
//        byte[] body = new byte[in.available()];
//        //把字节读取到数组中
//        in.read(body);
//        byte[] body = FileUtils.readFileToByteArray(file);
        //设置请求头
//        MultiValueMap headers = new HttpHeaders();
//        headers.add("Content-Disposition", "attchement;filename=" + file.getName());
//        headers.add("Content-Disposition", "attchement;filename=" + URLEncoder.encode(file.getName(), "UTF-8"));
        //设置响应状态
//        HttpStatus statusCode = HttpStatus.OK;
//        in.close();
//        ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(body, headers, statusCode);
//        return entity;//返回
    }
}
